package org.szy.plugin.sys.service.impl;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.ProxyComponent;
import org.szy.core.common.exception.BaseException;
import org.szy.core.common.page.QueryPage;
import org.szy.core.common.param.CommonStatusParam;
import org.szy.core.common.service.impl.BaseServiceImpl;
import org.szy.plugin.sys.entity.SysRole;
import org.szy.plugin.sys.mapper.SysRoleMapper;
import org.szy.plugin.sys.param.SysRoleQueryParam;
import org.szy.plugin.sys.service.SysRoleService;

import static org.szy.plugin.sys.entity.table.SysRoleTableDef.SYS_ROLE;

/**
 * 系统角色Service接口实现类
 *
 * @author wangbuhao
 * @since 2023-06-26 20:06:27
 **/
@ProxyComponent
public class SysRoleServiceImpl extends BaseServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Inject
    private SysRoleMapper sysRoleMapper;

    @Override
    public QueryWrapper qwByQuery(SysRoleQueryParam param) {
        QueryWrapper qw = new QueryWrapper();
        if (StrUtil.isNotEmpty(param.getColumnField())) {
            qw.select(param.getColumnField().split(","));
        }
        if (StrUtil.isNotEmpty(param.getName())) qw.and(SYS_ROLE.NAME.eq(param.getName()));
        if (StrUtil.isNotEmpty(param.getNameLike())) qw.and(SYS_ROLE.NAME.like(param.getNameLike()));
        if (StrUtil.isNotEmpty(param.getCode())) qw.and(SYS_ROLE.CODE.eq(param.getCode()));
        if (StrUtil.isNotEmpty(param.getCodeLike())) qw.and(SYS_ROLE.CODE.like(param.getCodeLike()));
        if (param.getStatus() != null) qw.and(SYS_ROLE.STATUS.eq(param.getStatus()));
        return qw;
    }

    @Override
    public Page<SysRole> page(QueryPage<SysRole> page, SysRoleQueryParam param) {
        QueryWrapper qw = qwByQuery(param);

        // 正序排序
        if (StrUtil.isEmpty(page.getAscField())) qw.orderBy(SYS_ROLE.SORT.asc());
        else qw.orderBy(page.getAscField().split(","));
        // 倒叙排序
        if (StrUtil.isNotEmpty(page.getDescField())) {
            for (String desc : page.getDescField().split(",")) {
                qw.orderBy(new QueryColumn(desc).desc());
            }
        }

        return this.page(page, qw);
    }

    @Override
    public boolean add(SysRole entity) {
        boolean exists = exists(SYS_ROLE.CODE.eq(entity.getCode()));
        if (exists) throw new BaseException("角色标识已存在！");
        if (entity.getSort() == null) entity.setSort(Convert.toInt(count(), 0) + 1);
        return this.save(entity);
    }

    @Override
    public boolean edit(SysRole entity) {
        boolean exists = exists(SYS_ROLE.CODE.eq(entity.getCode()).and(SYS_ROLE.ID.ne(entity.getId())));
        if (exists) throw new BaseException("角色标识已存在！");
        return updateById(entity);
    }

    @Override
    public boolean modifyStatus(CommonStatusParam param) {
        SysRole entity = UpdateEntity.of(SysRole.class, param.getId());
        entity.setStatus(param.getStatus());
        return sysRoleMapper.update(entity) > 0;
    }


}
